package com.zhiyou.hospital.ssh.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zhiyou.hospital.ssh.model.UserModel;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by zhouyanchao on 2017/10/16.
 * Descr: baseaction，将所有action通用的信息放到这里，例如获取登录用户的基本信息
 *
 */
public class BaseAction extends ActionSupport {

	final String REQ_GET = "get";
	final String REQ_POST = "post";

	final String VERIFY_LOGIN = "_session_login_verify_captcha";//存放登录验证码的属性名

	final String SESSION_USER_LOGINNAME = "_session_user_login_name";//session中的用户登录名
	final String SESSION_USER_ID = "_session_user_login_id";//session中的用户id

	final String SESSION_PERSSLIST = "_session_user_permission_list";//当前用户permission列表


	public HttpSession getSession(){
		return ServletActionContext.getRequest().getSession(true);
	}

	/**
	 * Created by zhouyanchao on 2017/10/17
	 * Descr: 获取当前已登录用户名
	 *
	 */
	public String getCurrentUserLoginName(){
		Map<String,Object> sessionMap = ActionContext.getContext().getSession();
		Object obj = sessionMap.get(SESSION_USER_LOGINNAME);
		if(obj==null){
			return "";
		}else{
			return obj.toString();
		}
	}

	public int getCurrentUserId(){
		Map<String,Object> sessionMap = ActionContext.getContext().getSession();
		Object obj = sessionMap.get(SESSION_USER_ID);
		if(obj==null){
			return 0;
		}else{
			return (Integer)obj;
		}
	}

	public void loginInit(UserModel userModel){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(true);
		session.setAttribute(SESSION_USER_LOGINNAME,userModel.getLoginName());
		session.setAttribute(SESSION_USER_ID,userModel.getId());
	}


	public Object getSessionAttribute(String name){
		Map<String,Object> datas= ActionContext.getContext().getSession();
		return datas.get(name);
	}
}
